import { LockOutlined, UserOutlined } from "@ant-design/icons";
import { Button, Form, Input, Space, message } from "antd";
import styles from './index.less';
import { login, getUserLimits } from "../../api/services";
import { history } from "umi";

const Login = () => {
  const user = JSON.parse(localStorage.getItem('user') as any);

  const onFinish = (values: any) => {
    login({ userNumber: values.usernumber, password: values.password })
      .then((res: any) => {
        console.log(res);
        if (res.status == 200) {
          message.success(res.msg);
          // history.push('/home');
          window.location.href = "http://localhost:8000/home";
          localStorage.setItem('user', JSON.stringify(res.data));
          localStorage.setItem('token', res.token);

          getUserLimits({
            roleId: res.data.user_roleid
          }).then((res: any) => {
            console.log(res);
            localStorage.setItem('admitRequest', res.admitRequest);
          })
        }
      })
  }
  return (
    <div className={styles.loginContainer}>
      <div className={styles.formContainer}>
        <div className={styles.title}>生产企业管理系统</div>
        <Form name="normal_login" className={styles.form} onFinish={onFinish}>
          <Form.Item
            name="usernumber"
            rules={[
              {
                required: true,
                message: "请输入账号!",
              },
            ]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="请输入账号"

            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[
              {
                required: true,
                message: "请输入密码",
              },
            ]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="请输入密码"
            />
          </Form.Item>
          <Form.Item>
            <Space size="large" style={{ width: '100%', justifyContent: 'center' }}>
              <Button
                type="primary"
                htmlType="submit"
                style={{ width: 250, height: 40, borderRadius: 5 }}
              >
                登录
              </Button>

            </Space>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Login;